package com.west.xu.tdmq.config;

import com.west.xu.tdmq.domain.Notice;
import org.apache.pulsar.client.api.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
@ConfigurationProperties(prefix = "tdmq")
public class PulsarConfig {

    @Resource
    private PulsarProperties pulsarProperties;

    /**
     *
     */
    @Value("${tdmq.notice.topic}")
    private String noticeTopic;
    @Value("${tdmq.notice.producerName}")
    private String noticeProducerName;
    /**
     *
     */

    /**
     * @return
     * @throws PulsarClientException
     */
    @Bean(name = "pulsarClient")
    public PulsarClient pulsarClient() throws PulsarClientException {
        return PulsarClient.builder()
                .serviceUrl(pulsarProperties.getServiceUrl())
//                .listenerName(pulsarProperties.getListenerName())
//                .authentication(AuthenticationFactory.token(pulsarProperties.getToken()))
                .build();
    }

    @Bean(name = "noticeProducer")
    public Producer<Notice> noticeProducer() throws PulsarClientException {
        return pulsarClient()
                .newProducer(Schema.JSON(Notice.class))
                .topic(noticeTopic)
                .producerName(noticeProducerName)
                .create();
    }
}
